package cn.hinglo.gameclaw.user.service;

import cn.hinglo.gameclaw.data.utils.FieldUtils;
import cn.hinglo.gameclaw.data.utils.IdUtils;
import cn.hinglo.gameclaw.data.utils.JwtUtil;
import cn.hinglo.gameclaw.im.service.ImService;
import cn.hinglo.gameclaw.user.bean.User;
import cn.hinglo.gameclaw.user.bean.UserInfo;
import cn.hinglo.gameclaw.user.dao.LoginDao;
import cn.hinglo.gameclaw.user.entity.ClientLogin;
import cn.hinglo.gameclaw.user.entity.Users;
import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.SecureUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * @createTime: 2017/12/28
 * @author: HingLo
 * @description: 登录服务
 */
@Service
public class LoginService {
    /**
     * 登录的访问层
     */
    private final LoginDao loginDao;
    /***
     * 即时通讯的服务层
     */
    private final ImService imService;

    @Autowired
    public LoginService(LoginDao loginDao, ImService imService) {
        this.loginDao = loginDao;
        this.imService = imService;
    }

    public String checkUserIdService(String userId) {
        return loginDao.findByIdUserIdDao(userId);
    }

    /**
     * 后台管理员登录
     *
     * @param users 登录的用户实体信息，包括用户名，密码
     * @return 登录成功的token
     */
    public String loginMethodService(Users users) {
        User user1 = loginDao.loginMethodDao(users.setPassword(SecureUtil.md5(users.getPassword())));
        if (user1 != null) {
            return JwtUtil.createJWT(user1.getId(), user1.toString());
        }
        return null;

    }

    /***
     * 用户登录的服务层
     * @return 返回登录后的token
     */
    @Transactional(rollbackFor = RuntimeException.class)
    public String loginMethodService(ClientLogin c) {
        String token;
        //判断该信息是否在系统中
        String userId = loginDao.findByIdUserIdDao(c.getId());
        if (StrUtil.isBlank(userId)) {
            User user = new User();
            user.setId(c.getId());
            user.setType(c.getTypes());
            user.setStatus(true);
            user.setDate(new Date());
            user.setUsername(c.getNikeName());
            //************************同步token与用户的Id到服务层********************************//
            token = imService.createAccIdService(c.getId(), c.getNikeName());
            //token = "fdjsklfjdasjflkdasjfkldasfjkas";
            if (token == null) {
                return null;
            }
            //设置与网易云交互的token
            user.setToken(token);
            //插入账号操作
            if (loginDao.insert(user) > 0) {

                UserInfo userInfo = new UserInfo();
                String code = IdUtils.getUerId();
                boolean bool = true;
                while (bool) {
                    List<UserInfo> list = loginDao.findUid(code);
                    if (list.isEmpty()) {
                        bool = false;
                    } else {
                        code = IdUtils.getUerId();
                    }
                }
                //设置用户唯一UID与邀请码是相同的
                userInfo.setUid(code);
                userInfo.setUserId(c.getId());
                userInfo.setSex(c.getSex());
                userInfo.setNikeName(c.getNikeName());
                userInfo.setHeader(c.getIcon());
                userInfo.setGold(0);
                userInfo.setScore(0);
                //初始化默认赠送的钻石数量
                userInfo.setGiveGold(FieldUtils.GOLD);
                //插入信息操作
                if (loginDao.addUserInfo(userInfo) > 0) {
                    return token;
                } else {
                    //如果操作失败
                    return null;
                }
            }
        } else {
            //获取token
            token = loginDao.selectByPrimaryKey(userId).getToken();
        }
        return token;
    }

}
